﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using WsusWebAdmin.Web.Controllers;
using Microsoft.Practices.Unity.StaticFactory;
using Microsoft.UpdateServices.Administration;
using WsusWebAdmin.WsusWrappers;
using System.Net;
using Microsoft.UpdateServices.Internal;
using System.Text.RegularExpressions;
using Telerik.Web.Mvc;
using WsusWebAdmin.Web.Wsus;

namespace WsusWebAdmin.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static IUnityContainer Container { get; private set; }

        public static void RegisterRoutes(RouteCollection routes)
        {            
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("ApiRemoting30/{*pathInfo}");

            /*routes.MapRoute(
                "UpdatesUnapproved",
                "Updates/Unapproved",
                new { controller = "Updates", action = "Unapproved" }
            );*/

            Routes.RegisterRoutes(RouteTable.Routes);

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );
            
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
                       
            InitializeUnity();
            SetupControllerFactory();

            InitializeTelerik();
        }        

        private void InitializeTelerik()
        {
            WebAssetDefaultSettings.ScriptFilesPath = "~/Scripts/Telerik";
            WebAssetDefaultSettings.StyleSheetFilesPath = "~/Content/Telerik";
        }

        private void SetupControllerFactory()
        {
            ControllerBuilder.Current.SetControllerFactory(typeof(ControllerFactory));
        }

        private void InitializeUnity()
        {
            Container = new UnityContainer();
            Container.AddNewExtension<StaticFactoryExtension>();

            Container.Configure<IStaticFactoryConfiguration>()
                     .RegisterFactory<IUpdateServer>(c => {
                         var wrapper = new HttpSessionStateWrapper(HttpContext.Current.Session);
                         return Wsus.UpdateServerFactory.GetInstance(wrapper.Typed().Credentials, wrapper.Typed().WsusHost);
                     });

            Container.RegisterType<IWsusDataProvider, FileDataProvider>(
                                        new InjectionConstructor(Context.Server.MapPath("~/servers.xml"))
                                    );                
        }
    }
}